package pe.edu.unmsm.pacis.shca.controller;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import pe.edu.unmsm.pacis.shca.domain.entity.User;
import pe.edu.unmsm.pacis.shca.domain.exception.DomainException;
import pe.edu.unmsm.pacis.shca.service.AgentService;
import pe.edu.unmsm.pacis.shca.util.UtilWeb;

@ManagedBean
@SessionScoped
@Controller
public class LoginController implements Serializable {

	private static final long serialVersionUID = 1L;
	
	private String login;
	private String password;
	private String replyValidate;
	
	@Autowired
	AgentService agentService;
	
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String loginValidate(){
		try {
			User user = this.agentService.loginValidate(); 
			if(user==null)
				this.replyValidate = "User not found";
			else
				this.replyValidate = "User ok: " + user.getName() + " " + user.getLastName();
		} catch (DomainException e) {
			this.replyValidate = UtilWeb.exceptionFromProperties(e);
		}
		return null;
	}
	
	public String getReplyValidate() {
		return replyValidate;
	}
	public void setReplyValidate(String replyValidate) {
		this.replyValidate = replyValidate;
	}
	
}
